﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2010 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器 V4.3.3.6 自动创建于 2010/4/15 9:33:34
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using IBS_Test2.SqlServerDAL;

namespace IBSSqlServerDAL
{
    /// <summary>
    /// 数据访问类 TimeTable
    /// </summary>
    public class TimeTable : DALHelper
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(IBSModel.TimeTable model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO TimeTable(");
            strSql.Append("OwnLineIBDMID,TimeTableName,BeginDate,EndDate,StartStationName,EndStationName,TokenAmount,TurnAmount,ShowRowsAmount,ShowColsAmount,Level,Type,Valid,CreateDate)");
            strSql.Append(" VALUES (");
            strSql.Append("@in_OwnLineIBDMID,@in_TimeTableName,@in_BeginDate,@in_EndDate,@in_StartStationName,@in_EndStationName,@in_TokenAmount,@in_TurnAmount,@in_ShowRowsAmount,@in_ShowColsAmount,@in_Level,@in_Type,@in_Valid,@in_CreateDate)");
            DbParameter[] cmdParms = {
                                         IBS_Test2Helper.CreateInDbParameter("@in_OwnLineIBDMID", DbType.AnsiString, model.OwnLineIBDMID),
                                         IBS_Test2Helper.CreateInDbParameter("@in_TimeTableName", DbType.AnsiString, model.TimeTableName),
                                         IBS_Test2Helper.CreateInDbParameter("@in_BeginDate", DbType.String, model.BeginDate),
                                         IBS_Test2Helper.CreateInDbParameter("@in_EndDate", DbType.String, model.EndDate),
                                         IBS_Test2Helper.CreateInDbParameter("@in_StartStationName", DbType.AnsiString, model.StartStationName),
                                         IBS_Test2Helper.CreateInDbParameter("@in_EndStationName", DbType.AnsiString, model.EndStationName),
                                         IBS_Test2Helper.CreateInDbParameter("@in_TokenAmount", DbType.Int32, model.TokenAmount),
                                         IBS_Test2Helper.CreateInDbParameter("@in_TurnAmount", DbType.Int32, model.TurnAmount),
                                         IBS_Test2Helper.CreateInDbParameter("@in_ShowRowsAmount", DbType.Int32, model.ShowRowsAmount),
                                         IBS_Test2Helper.CreateInDbParameter("@in_ShowColsAmount", DbType.Int32, model.ShowColsAmount),
                                         IBS_Test2Helper.CreateInDbParameter("@in_Level", DbType.Int32, model.Level),
                                         IBS_Test2Helper.CreateInDbParameter("@in_Type", DbType.AnsiString, model.Type),
                                         IBS_Test2Helper.CreateInDbParameter("@in_Valid", DbType.Boolean, model.Valid),
                                         IBS_Test2Helper.CreateInDbParameter("@in_CreateDate", DbType.String, model.CreateDate)};

            return IBS_Test2Helper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), cmdParms);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(IBSModel.TimeTable model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE TimeTable SET ");
            strSql.Append("OwnLineIBDMID=@in_OwnLineIBDMID,");
            strSql.Append("TimeTableName=@in_TimeTableName,");
            strSql.Append("BeginDate=@in_BeginDate,");
            strSql.Append("EndDate=@in_EndDate,");
            strSql.Append("StartStationName=@in_StartStationName,");
            strSql.Append("EndStationName=@in_EndStationName,");
            strSql.Append("TokenAmount=@in_TokenAmount,");
            strSql.Append("TurnAmount=@in_TurnAmount,");
            strSql.Append("ShowRowsAmount=@in_ShowRowsAmount,");
            strSql.Append("ShowColsAmount=@in_ShowColsAmount,");
            strSql.Append("Level=@in_Level,");
            strSql.Append("Type=@in_Type,");
            strSql.Append("Valid=@in_Valid,");
            strSql.Append("CreateDate=@in_CreateDate");
            strSql.Append(" WHERE ID=@in_ID");
            DbParameter[] cmdParms = {
                                         IBS_Test2Helper.CreateInDbParameter("@in_OwnLineIBDMID", DbType.AnsiString, model.OwnLineIBDMID),
                                         IBS_Test2Helper.CreateInDbParameter("@in_TimeTableName", DbType.AnsiString, model.TimeTableName),
                                         IBS_Test2Helper.CreateInDbParameter("@in_BeginDate", DbType.String, model.BeginDate),
                                         IBS_Test2Helper.CreateInDbParameter("@in_EndDate", DbType.String, model.EndDate),
                                         IBS_Test2Helper.CreateInDbParameter("@in_StartStationName", DbType.AnsiString, model.StartStationName),
                                         IBS_Test2Helper.CreateInDbParameter("@in_EndStationName", DbType.AnsiString, model.EndStationName),
                                         IBS_Test2Helper.CreateInDbParameter("@in_TokenAmount", DbType.Int32, model.TokenAmount),
                                         IBS_Test2Helper.CreateInDbParameter("@in_TurnAmount", DbType.Int32, model.TurnAmount),
                                         IBS_Test2Helper.CreateInDbParameter("@in_ShowRowsAmount", DbType.Int32, model.ShowRowsAmount),
                                         IBS_Test2Helper.CreateInDbParameter("@in_ShowColsAmount", DbType.Int32, model.ShowColsAmount),
                                         IBS_Test2Helper.CreateInDbParameter("@in_Level", DbType.Int32, model.Level),
                                         IBS_Test2Helper.CreateInDbParameter("@in_Type", DbType.AnsiString, model.Type),
                                         IBS_Test2Helper.CreateInDbParameter("@in_Valid", DbType.Boolean, model.Valid),
                                         IBS_Test2Helper.CreateInDbParameter("@in_CreateDate", DbType.String, model.CreateDate),
                                         IBS_Test2Helper.CreateInDbParameter("@in_ID", DbType.Int32, model.ID)};
            return IBS_Test2Helper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), cmdParms);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM TimeTable ");
            strSql.Append(" WHERE ID=@in_ID");
            DbParameter[] cmdParms = {
                                         IBS_Test2Helper.CreateInDbParameter("@in_ID", DbType.Int32, ID)};

            return IBS_Test2Helper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), cmdParms);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM TimeTable");
            strSql.Append(" WHERE ID=@in_ID");
            DbParameter[] cmdParms = {
                                         IBS_Test2Helper.CreateInDbParameter("@in_ID", DbType.Int32, ID)};

            object obj = IBS_Test2Helper.ExecuteScalar(CommandType.Text, strSql.ToString(), cmdParms);
            return IBS_Test2Helper.GetInt(obj) > 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public IBSModel.TimeTable GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM TimeTable ");
            strSql.Append(" WHERE ID=@in_ID");
            DbParameter[] cmdParms = {
                                         IBS_Test2Helper.CreateInDbParameter("@in_ID", DbType.Int32, ID)};

            IBSModel.TimeTable model = null;
            using (DbDataReader dr = IBS_Test2Helper.ExecuteReader(CommandType.Text, strSql.ToString(), cmdParms))
            {
                while (dr.Read())
                {
                    model = GetModel(dr);
                }
                return model;
            }
        }

        /// <summary>
        /// 获取泛型数据列表
        /// </summary>
        public List<IBSModel.TimeTable> GetList()
        {
            StringBuilder strSql = new StringBuilder("SELECT * FROM TimeTable");
            using (DbDataReader dr = IBS_Test2Helper.ExecuteReader(CommandType.Text, strSql.ToString(), null))
            {
                List<IBSModel.TimeTable> lst = GetList(dr);
                return lst;
            }
        }

        /// <summary>
        /// 得到数据条数
        /// </summary>
        public int GetCount(string condition)
        {
            return IBS_Test2Helper.GetCount("TimeTable", condition);
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public List<IBSModel.TimeTable> GetPageList(int pageSize, int pageIndex, string fldSort, bool fldDir, string condition)
        {
            using (DbDataReader dr = IBS_Test2Helper.GetPageList("TimeTable", pageSize, pageIndex, fldSort, fldDir, condition))
            {
                List<IBSModel.TimeTable> lst = GetList(dr);
                return lst;
            }
        }

        #region -------- 私有方法，通常情况下无需修改 --------

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private IBSModel.TimeTable GetModel(DbDataReader dr)
        {
            IBSModel.TimeTable model = new IBSModel.TimeTable();
            model.ID = IBS_Test2Helper.GetInt(dr["ID"]);
            model.OwnLineIBDMID = IBS_Test2Helper.GetString(dr["OwnLineIBDMID"]);
            model.TimeTableName = IBS_Test2Helper.GetString(dr["TimeTableName"]);
            model.BeginDate = IBS_Test2Helper.GetDateTime(dr["BeginDate"]);
            model.EndDate = IBS_Test2Helper.GetDateTime(dr["EndDate"]);
            model.StartStationName = IBS_Test2Helper.GetString(dr["StartStationName"]);
            model.EndStationName = IBS_Test2Helper.GetString(dr["EndStationName"]);
            model.TokenAmount = IBS_Test2Helper.GetInt(dr["TokenAmount"]);
            model.TurnAmount = IBS_Test2Helper.GetInt(dr["TurnAmount"]);
            model.ShowRowsAmount = IBS_Test2Helper.GetInt(dr["ShowRowsAmount"]);
            model.ShowColsAmount = IBS_Test2Helper.GetInt(dr["ShowColsAmount"]);
            model.Level = IBS_Test2Helper.GetInt(dr["Level"]);
            model.Type = IBS_Test2Helper.GetString(dr["Type"]);
            model.Valid = IBS_Test2Helper.GetBool(dr["Valid"]);
            model.CreateDate = IBS_Test2Helper.GetDateTime(dr["CreateDate"]);
            return model;
        }

        /// <summary>
        /// 由DbDataReader得到泛型数据列表
        /// </summary>
        private List<IBSModel.TimeTable> GetList(DbDataReader dr)
        {
            List<IBSModel.TimeTable> lst = new List<IBSModel.TimeTable>();
            while (dr.Read())
            {
                lst.Add(GetModel(dr));
            }
            return lst;
        }

        #endregion

        #region User Code

        /// <summary>
        /// 得到一个在用时刻表对象实体
        /// </summary>
        public int GetValidModel(int id, string type)
        {
            string strSql =
                string.Format(
                    @"SELECT [ID]
                      FROM [IBS_Test].[dbo].[TimeTable]
                      WHERE [OwnLineIBDMID] = @ownLineIBDMID
                      AND [Type] = @type");
            DbParameter[] cmdParms = {
                                         IBS_Test2Helper.CreateInDbParameter("@ownLineIBDMID", DbType.Int32, id),
                                         IBS_Test2Helper.CreateInDbParameter("@type", DbType.String, type)
                                     };

            object obj = IBS_Test2Helper.ExecuteScalar(CommandType.Text, strSql, cmdParms);
            return IBS_Test2Helper.GetInt(obj);
        }

        #endregion
    }
}